﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Collections;
using System.Diagnostics;
using DotNet.Common.Kit;
using TB.Spider.Dianpu;

namespace taobaoSpider
{
    /// <summary>
    /// 定义错误显示的委托
    /// </summary>
    /// <param name="clsName">类名</param>
    /// <param name="errMessage">错误消息名</param>
    

    public partial class Form1 : Form
    {
        public delegate void InitControlDataHandler(object sender,TBShowLogEventArgs e);
        private Object thisLock = new Object();  //进程锁变量
        string exePath = Application.StartupPath + "\\";
        ArrayList listFileKey = new ArrayList();
        Dictionary<string, string> nickSum = new Dictionary<string, string>(); //本次得到所有关键词掌柜名
        int threadCount = 0;
        LogerHelper logger = new LogerHelper(DirType.Local, MessageType.Exception);


        //KeySearchDianpu ksDp = new KeySearchDianpu();
        ShopIndex shop = new ShopIndex();

        public Form1()
        {
            InitializeComponent();
            //ksDp.URLShift+=new UrlShiftEventHandler(ksDp_URLShift);
            shop.PrintLog+=new TBEventHandler(shop_PrintLog);
            shop.IPShift+=new TBEventHandler(shop_IPShift);
        }
        private void shop_PrintLog(object sender, TBShowLogEventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;//以线程非安全的方式调用该委托方法。
            lblInfo.Text = e.Msg;
        }
        private void shop_IPShift(object sender, TBShowLogEventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;//以线程非安全的方式调用该委托方法。
            lblIpInfo.Text = e.Msg;
        }
        
        public void ShowErrorList(object sender, ShowErrorLogEventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;//以线程非安全的方式调用该委托方法。
            try
            {
                if (sender is Label)
                {
                    if (((Label)sender).Name == "lblStartTime")
                    {
                        lblStartTime.Text = lblStartTime.Text + e.ErrMessage;
                    }
                    if (((Label)sender).Name == "lblEndTime")
                    {
                        lblEndTime.Text = lblEndTime.Text + e.ErrMessage;
                    }
                    if (((Label)sender).Name == "lblUseTime")
                    {
                        lblUseTime.Text = "已用时间:" + e.ErrMessage + " 分";
                    }
                    if (((Label)sender).Name == "lblSum")
                    {
                        lblSum.Text = "掌柜总数:" + e.ErrMessage + " 条";
                    }
                    if ((((Label)sender).Name == "lblInfo"))
                    {
                        lblInfo.Text = e.ErrDateTime + " " + e.ErrPosition + "\r\n" + e.ErrMessage;
                    }
                    if ((((Label)sender).Name == "lblIpInfo"))
                    {
                        try
                        {
                            lblIpInfo.Text = e.ErrPosition + "\r\n" + e.ErrMessage;
                        }
                        catch (Exception ex)
                        {
                            logger.WriteErrorLog(ex.StackTrace + ex.Message);
                        }
                    }
                }
                if (sender is DataGridView)
                {
                    if ((((DataGridView)sender).Name == "dgvShow"))
                    {
                        int index = dgvShow.Rows.Count;
                        //序号 掌柜名 掌柜旺旺 所在地 店铺首页  信用等级 创店日期 店铺版本 设计师 采集时间
                        string[] row = new string[] {index.ToString(),e.DPInfo.nickname,
                                                 "旺旺",e.DPInfo.location,
                                                 e.DPInfo.dpURL,e.DPInfo.dpXy,
                                                 e.DPInfo.createTime,e.DPInfo.dpVer,
                                                 e.DPInfo.sjName,DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToLongTimeString()};
                        // lblInfo.Text = e.ErrDateTime + " " + e.ErrPosition + "\r\n" + e.ErrMessage;
                        dgvShow.Rows.Add(row);
                    }
                    //if ((((DataGridView)sender).Name == "dgvDesigner"))
                    //{
                    //    string[] row = e.ErrMessage.Split(' ');
                    //    dgvDesigner.Rows.Add(row);
                    //}
                    if ((((DataGridView)sender).Name == "dgvKeyword"))
                    {
                        int pos = 0;
                        foreach (DataGridViewRow rows in dgvKeyword.Rows)
                        {
                            if (rows.Cells[1].Value.ToString() == e.ErrPosition)
                            {
                                pos = dgvKeyword.Rows.IndexOf(rows);
                                break;
                            }
                        }
                        string[] rowFlag = e.ErrMessage.Split(':'); //0:则表示是总数  1:则表示是当前余下数目，是变化的。 0:50  1:20这样的格式。
                        if (rowFlag[0] == "0")
                        {
                            dgvKeyword.Rows[pos].Cells[2].Value = rowFlag[1];
                        }
                        if (rowFlag[0] == "1")
                        {
                            dgvKeyword.Rows[pos].Cells[3].Value = rowFlag[1];
                        }
                    }
                }
                if (sender.ToString() == "closeSys")
                {
                    this.Invoke(new CloseSystemEventHandler(ShowCloseSystemForm), new object());
                }
            }
            catch (Exception ex)
            {
                logger.WriteErrorLog(ex.StackTrace + ex.Message);
            }
        }

        public void ShowCloseSystemForm(object sender)
        {
            frmCloseSystem frm = new frmCloseSystem();
            DialogResult drRet = frm.ShowDialog(this);
        }
        /// <summary>
        /// 将ShopIndex类的值初始化到dianpu实体类，以便写入数据库用。
        /// </summary>
        /// <param name="shop"></param>
        /// <param name="dpEntity"></param>
        /// <param name="hanye"></param>
        /// <param name="loc"></param>
        private void InitDpEntity(HtmlShopIndex shop,TBWS.dianpu dpEntity, string hanye, string loc)
        {
            dpEntity.shopId = Convert.ToInt32(shop.ShopID);
            dpEntity.shopTitle = shop.ShopTitle;
            dpEntity.nickname = shop.NickName;
            dpEntity.userId = shop.UserID;

            dpEntity.dpVer = shop.DPVer;
            dpEntity.sjName = shop.SJName;
            dpEntity.templateID = shop.TemplateID;
            dpEntity.dpXy = shop.XyRank;

            dpEntity.dpURL = shop.ShopURL;
            dpEntity.kefu = "";
            dpEntity.hanye = hanye;
            dpEntity.location = loc;

            dpEntity.createTime = shop.CreateDate;
            dpEntity.dgTime = "";
            dpEntity.cjTime = DateTime.Now.ToShortDateString();

            //dpEntity.cjNum = "";
            dpEntity.reverse2 = "";
            dpEntity.reverse3 = "";
        }
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }


        #region 线程处理部分 
        private void DesignerThread(object ms)
        {
            DateTime dt1 = DateTime.Now;
            DesignerCjThreadParameter parameter = ms as DesignerCjThreadParameter;
            int startPageNum = parameter.StartPageNum;
            int endPageNum = parameter.EndPageNum;

            TBWS.Desinger designerWS = new TBWS.Desinger();
            TBWS.tb_wsSoapClient client = new TBWS.tb_wsSoapClient();

            ShowErrorLogEventArgs e = new ShowErrorLogEventArgs();
            e.ErrMessage = dt1.ToShortDateString() + " " + dt1.ToLongTimeString();
            this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), lblStartTime, e);

            HtmlDesigner designer = new HtmlDesigner(new ShowErrorLogEventHandler(ShowErrorList), ReadIniFile.IniReadValue("designer", "dl_designer"));
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                designer.DS.Clear();
                designer.InitDesigner(i);
                DataSet ds = designer.DS;
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    designerWS.nickname = row["nickname"].ToString();
                    designerWS.nickname_idnum = row["nickname_idnum"].ToString();
                    designerWS.jianjie = row["jianjie"].ToString();
                    designerWS.templateNum = Convert.ToInt32(row["templateNum"]);
                    designerWS.rzDate = row["rzDate"].ToString();

                    designerWS.cjDate = DateTime.Now.ToShortDateString();
                    designerWS.resever1 = "";
                    designerWS.resever2 = "";
                    try
                    {
                        client.InsertDesigner(designerWS);
                    }
                    catch (Exception ex)
                    {
                        GlobalFun.WriteErrorLog("Form1:DesignerThread()=>catch", "\r\n设计师ID---" + designerWS.nickname_idnum + "\r\n" + ex.Message);
                        GlobalFun.WriteErrorLog("", "-------------------------------------------------------------------");
                    }
                }
            }
        }
        private void TemplateThread(object ms)
        {
            DateTime dt1 = DateTime.Now;
            DesignerCjThreadParameter parameter = ms as DesignerCjThreadParameter;
            int startPageNum = parameter.StartPageNum;
            int endPageNum = parameter.EndPageNum;

            TBWS.Template template_ws = new TBWS.Template();
            TBWS.tb_wsSoapClient client = new TBWS.tb_wsSoapClient();

            ShowErrorLogEventArgs e = new ShowErrorLogEventArgs();
            e.ErrMessage = dt1.ToShortDateString() + " " + dt1.ToLongTimeString();
            this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), lblStartTime, e);

            HtmlTemplate template = new HtmlTemplate(new ShowErrorLogEventHandler(ShowErrorList), ReadIniFile.IniReadValue("template", "templateItem"));
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                template.DS.Clear();
                template.InitTemplatePage(i);
                DataSet ds = template.DS;
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    template_ws.template_idnum = row["template_idnum"].ToString();
                    template_ws.nickname = row["nickname"].ToString();
                    template_ws.sellnum = (int)row["sellnum"];
                    template_ws.userVer = (row["userVer"]).ToString();
                    template_ws.price = (double)row["price"];

                    template_ws.title = row["title"].ToString();
                    template_ws.hangye = "";
                    template_ws.cjTime = DateTime.Now.ToShortDateString();
                    template_ws.updateTime = "";
                    template_ws.reserver1 = "";
                    template_ws.reserver2 = "";
                    try
                    {
                        client.InsertTemplate(template_ws);
                    }
                    catch (Exception ex)
                    {
                        GlobalFun.WriteErrorLog("Form1:TemplateThread()=>catch", "\r\n模板ID---" +template_ws.template_idnum+ "\r\n" + ex.Message);
                        GlobalFun.WriteErrorLog("", "-------------------------------------------------------------------");
                    }
                }
            }
        }
        public void ManyKeyWordThread(object ms)
        {
            DateTime dt1 = DateTime.Now;
            TBWS.dianpu dp = new TBWS.dianpu();
            TBWS.tb_wsSoapClient client = new TBWS.tb_wsSoapClient();
            MyThreadParameter parameter = ms as MyThreadParameter;
            string hy = parameter.HangYe;
            ShowErrorLogEventArgs e = new ShowErrorLogEventArgs();
            e.ErrMessage = dt1.ToShortDateString() + " " + dt1.ToShortTimeString();
            this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), lblStartTime, e);
            HtmlKeySearch keySearch = new HtmlKeySearch(new ShowErrorLogEventHandler(ShowErrorList), ReadIniFile.IniReadValue("keySearch", "seller"), ReadIniFile.IniReadValue("keySearch", "location"), ReadIniFile.IniReadValue("keySearch", "_bbtitle"));
            HtmlShopSearch shopSearch = new HtmlShopSearch(new ShowErrorLogEventHandler(ShowErrorList), ReadIniFile.IniReadValue("shopSearch", "shopurl"), ReadIniFile.IniReadValue("shopSearch", "nickname"));
            HtmlShopIndex shopIndex = new HtmlShopIndex(new ShowErrorLogEventHandler(ShowErrorList));

            lock (thisLock)
            {
                threadCount++;
            }
            foreach (string key in listFileKey)
            {
                keySearch.InitNickInfo(key, nickSum);
                int sum = 0; //减少一个写入成功标志的变量
                // this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), dgvKeyword, e);
                foreach (KeyValuePair<string, string> kvp in keySearch.NickLocationSum)
                {
                    try
                    {
                        shopSearch.NickName = kvp.Key;
                        shopSearch.NickNameVal = kvp.Value;
                        shopSearch.InitOneShopUrl();
                        if (shopSearch.ShopUrl != "")
                        {
                            shopIndex.ShopURL = shopSearch.ShopUrl;
                            int ret = shopIndex.InitDianpuIndex();
                            if (ret == 1)
                            {
                                sum++;
                                InitDpEntity(shopIndex, dp, hy, kvp.Value.Split('*')[0]);
                                client.InsertDianpu(dp);

                                e.DPInfo = dp;
                                this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), dgvShow, e);
                                e.ErrPosition = key;
                                e.ErrMessage = "1:" + (keySearch.NickLocationSum.Count - sum).ToString();//此处的“1:”与121行处的"0:"作为写入DataGridView列的标志。
                                this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), dgvKeyword, e);
                            }
                        }
                        e.ErrMessage = (DateTime.Now - dt1).TotalMinutes.ToString();
                        this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), lblUseTime, e);
                    }
                    catch (Exception ex)
                    {
                        e.ErrPosition = "Form1:ManyKeyWord():catch()";
                        e.ErrMessage = "掌柜名--" + kvp.Key + "--处发生错误===>" + ex.Message;
                        this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), lblInfo, e);

                        e.ErrMessage = "掌柜--" + dp.nickname + "--的信息更新到数据库失败";
                        this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), lblInfo, e);

                        // GlobalFun.WriteErrorLog(ex.StackTrace+"  "+ex.Message, e.ErrMessage);
                        logger.WriteErrorLog(ex.StackTrace + "  " + ex.Message + e.ErrMessage);

                    }
                }
            }
            lock (thisLock)
            {
                threadCount--;
            }
            if (threadCount == 0)
            {
                //this.btn_manyKeyWord.Enabled = true;此处不能直接访问主界面Button元素。线程间操作会有问题。
                //解决办法两种，一种用委托。
                this.btn_manyKeyWord.Enabled = true;
                e.ErrMessage = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
                this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), lblEndTime, e);
                string content = this.lblStartTime.Text + "\r\n" + this.lblSum.Text + "  " + this.lblUseTime.Text + "\r\n" + this.lblEndTime.Text;
                GlobalFun.Write("time.txt", true, content);
                GlobalFun.Write("time.txt", true, "---------------------------------");
                if (chb_closeComputer.Checked)
                {
                    this.Invoke(new CloseSystemEventHandler(ShowCloseSystemForm), new object());
                }
            }
        }
        #endregion  线程处理部分  结束

        #region 按钮事件处理部分
        private void btn_openFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "KeyWord File (*.key)|*.key";
            ofd.InitialDirectory = exePath + "keyword";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (listFileKey.Count > 0)
                {
                    listFileKey.Clear();
                    dgvKeyword.Rows.Clear();
                }
                txt_manyKeyWord.Text = ofd.SafeFileName.Split('.')[0];
                ArrayList list = GlobalFun.ReadFile(ofd.FileName);
                int index = 0;
                foreach (string key in list)
                {
                    if ((!key.Contains("//")) && (!listFileKey.Contains(key)))
                    {
                        listFileKey.Add(key);
                        index++;
                        string[] row = new string[] {index.ToString(),key,
                                                 "0","0"};
                        dgvKeyword.Rows.Add(row);
                    }
                }
            }
        }
        static int XX=0;
        private void k_NetError()
        {
            XX++;
            this.label1.Text = XX.ToString() ;
        }

        private void btn_BaoBei_Click(object sender, EventArgs e)
        {
            //ksDp.KeyWord = "打底衫";
            //ksDp.GetKeySearchDianpuByBrowser();
            int ipcount = 0;
            string time = "123";
            int x = 10;
            int y = 10;
                //string X = ShiftIPHelper.GetShiftIp(ref ipcount);
            shop.GetDianpuInfo("http://shop2004.taobao.com/");
            //ShiftIPHelper.UpdateIp("175.189.197.49", "useNum:10000", "vtime:100000");
            //MessageBox.Show(X);
        }
        private void btn_manyKeyWord_Click(object sender, EventArgs e)
        {
            if (txt_manyKeyWord.Text != "" && listFileKey.Count != 0)
            {
                btn_manyKeyWord.Enabled = false;
                string keyFileName = txt_manyKeyWord.Text;
                Thread manyKeyWordThread = new Thread(new ParameterizedThreadStart(ManyKeyWordThread));
                MyThreadParameter paramter = new MyThreadParameter(keyFileName);
                manyKeyWordThread.Start(paramter);
            }
            else
            {
                MessageBox.Show("请选择关键词文档");
            }
        }
        private void btn_ZdURL_Click(object sender, EventArgs e)
        {
            //此处没有委托参数可能会有问题。
            if (txt_shopURL.Text != "" && txt_shopURL.Text != "http://shop")
            {
                HtmlShopIndex shopIndex = new HtmlShopIndex(new ShowErrorLogEventHandler(this.ShowErrorList));
                shopIndex.ShopURL = txt_shopURL.Text;
                ShowErrorLogEventArgs ex = new ShowErrorLogEventArgs();
                int ret = shopIndex.InitDianpuIndex();
                if (ret == 1)
                {
                    TBWS.dianpu dp1 = new TBWS.dianpu();
                    TBWS.tb_wsSoapClient client1 = new TBWS.tb_wsSoapClient();
                    InitDpEntity(shopIndex, dp1, "", "");
                    client1.InsertDianpu(dp1);

                    ex.DPInfo = dp1;
                    ex.ErrPosition = "Form1:btn_ZdURL_Click()";
                    ex.ErrMessage = "掌柜--" + dp1.nickname + "--的信息更新到数据库成功";
                    this.Invoke(new ShowErrorLogEventHandler(ShowErrorList), dgvShow, ex);
                }
            }
            else
            {
                MessageBox.Show("请填写正确的掌柜首页URL");
            }
        }
        private void btn_moban_Click(object sender, EventArgs e)
        {
            txtStartPage.ReadOnly = false;
            txtEndPage.ReadOnly = false;
            if (txtStartPage.Text != "" && txtEndPage.Text != "")
            {
                btn_sjCj.Enabled = false;
                btn_manyKeyWord.Enabled = false;
                txtStartPage.ReadOnly = true;
                txtEndPage.ReadOnly = true;

                Thread templateThread = new Thread(new ParameterizedThreadStart(TemplateThread));
                DesignerCjThreadParameter paramter = new DesignerCjThreadParameter(Convert.ToInt32(txtStartPage.Text), Convert.ToInt32(txtEndPage.Text));
                templateThread.Start(paramter);
            }
        }
        private void btn_sjCj_Click(object sender, EventArgs e)
        {
            txtStartPage.ReadOnly = false;
            txtEndPage.ReadOnly = false;
            if (txtStartPage.Text != "" && txtEndPage.Text != "")
            {
                btn_sjCj.Enabled = false;
                btn_manyKeyWord.Enabled = false;
                txtStartPage.ReadOnly = true;
                txtEndPage.ReadOnly = true;

                Thread designerThread = new Thread(new ParameterizedThreadStart(DesignerThread));
                DesignerCjThreadParameter paramter = new DesignerCjThreadParameter(Convert.ToInt32(txtStartPage.Text), Convert.ToInt32(txtEndPage.Text));
                designerThread.Start(paramter);
            }
        }

        private void InitDpEntity(ShopIndex shop, TBWS.dianpu dpEntity)
        {
            dpEntity.shopId = Convert.ToInt32(shop.ShopID);
            dpEntity.shopTitle = shop.ShopTitle;
            dpEntity.nickname = shop.Nickname;
            dpEntity.userId = shop.UserID;

            dpEntity.dpVer = shop.DPVer;
            dpEntity.sjName = shop.SJName;
            dpEntity.templateID = shop.TemplateID;
            dpEntity.dpXy = shop.XyRank;

            dpEntity.dpURL = shop.ShopUrl;
            dpEntity.kefu = "";
            dpEntity.hanye = "";
            dpEntity.location ="";

            dpEntity.createTime = shop.CreateDate;
            dpEntity.dgTime = "";
            dpEntity.cjTime = DateTime.Now.ToShortDateString();
            dpEntity.reverse2 = "";
            dpEntity.reverse3 = "";
        }
        private void btn_Update_Click(object sender, EventArgs e)
        {
            if (txtDaysNum.Text != "")
            {
                string date = dateTimePicker1.Value.ToShortDateString();
                string daysNum = txtDaysNum.Text;
                txtDaysNum.Enabled = false;
                btn_Update.Enabled = false;
                timer1.Enabled = true;
                lblStartTime.Text =lblStartTime.Text+ DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToLongTimeString();
                //DateTime dt = DateTime.Parse("2009年3月31日").ToShortDate();

                Thread updateShopThread = new Thread(new ParameterizedThreadStart(UpdateShopThread));
                updateShopThread.Start();
            }
        }

        private void UpdateShopThread(object sender)
        {
            TBShowLogEventArgs e = new TBShowLogEventArgs();
            TBWS.tb_wsSoapClient client = new TBWS.tb_wsSoapClient();
            TBWS.dianpu dianpu = new TBWS.dianpu();
            int noGxSum = 0;  //总的条数
            DataSet ds = client.GetGxDpURL(ref noGxSum, "2013-10-10", 0);
            e.Msg = "更新记录数：" + noGxSum;
            this.Invoke(new InitControlDataHandler(InitLabelValue), lblSum, e);
            int index = 0;
            while (noGxSum > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    int ret = shop.GetDianpuInfo(dr[0].ToString());
                    if(ret==1)
                    {
                        index++;
                        e.ShopInfo = shop;
                        e.ShopInfo.Loc = dr[2].ToString();
                        e.Msg = index.ToString()+"---ShopUrl:"+shop.ShopUrl;
                        if (dr["nickname"].ToString() == shop.Nickname)
                        {
                            InitDpEntity(shop, dianpu);
                            try
                            {
                                client.InsertDianpu(dianpu);
                                this.Invoke(new InitControlDataHandler(InitLabelValue), lblInfo, e);
                                this.Invoke(new InitControlDataHandler(InitDataGridView), dgvShow, e);
                            }
                            catch (Exception ex)
                            {
                                LocLoggerHelper.WriteException(ex);
                            }
                        }
                        else
                        {
                            LocLoggerHelper.WriteErrorLog("nickname!=shop",dr["nickname"].ToString()+":"+dr["dpURL"].ToString());
                        }
                    }
                    else 
                    {
                        LocLoggerHelper.WriteErrorLog("shop",shop.ShopUrl);
                    }
                }
                ds = client.GetGxDpURL(ref noGxSum, "2013-10-10", 0);
                e.Msg = "更新记录数：" + noGxSum;
                this.Invoke(new InitControlDataHandler(InitLabelValue), lblSum, e);
            }
        }

        public void InitDataGridView(object sender, TBShowLogEventArgs e)
        {
            if ((((DataGridView)sender).Name == "dgvShow"))
            {
                int index = dgvShow.Rows.Count;
                //序号 掌柜名 掌柜旺旺 所在地 店铺首页  信用等级 创店日期 店铺版本 设计师 采集时间
                string[] row = new string[] {index.ToString(),e.ShopInfo.Nickname,
                                                 "旺旺",e.ShopInfo.Loc,
                                                 e.ShopInfo.ShopUrl,e.ShopInfo.XyRank,
                                                 e.ShopInfo.CreateDate,e.ShopInfo.DPVer,
                                                 e.ShopInfo.SJName,DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToLongTimeString()};
                // lblInfo.Text = e.ErrDateTime + " " + e.ErrPosition + "\r\n" + e.ErrMessage;
                dgvShow.Rows.Add(row);
            }
        }
        public void InitLabelValue(object sender, TBShowLogEventArgs e)
        {
            ((Label)sender).Text = e.Msg;
        }
        #endregion 按钮事件处理部分 结束

        #region Form事件处理部分

        private void Form1_Load(object sender, EventArgs e)
        {
            cmb_HangYe.Text = "通用";
            IniHelper ini = new IniHelper(System.Windows.Forms.Application.StartupPath + "\\HtmlNodeXPath.ini");
            string cjMode = ini.GetKeyValue("ProxyUseFlag", "proxy") + ini.GetKeyValue("ProxyUseFlag", "closeSys");  //采集模式与关机模式组合11,10,00,01
            if (cjMode == "11") //启用代理 ，不启用关机
            {
                lblInfo.Text = "启用代理IP模式，不启用关机";
            }
            else if (cjMode == "10")  //启用代理，启用关机
            {
                lblInfo.Text = "启用代理IP模式，启用关机";
            }
            else if (cjMode == "00") //不启用代理，启用关机
            {
                lblInfo.Text = "启用路由IP模式，启用关机";
            }
            else if (cjMode == "01")  //不启用代理，不启用关机
            {
                lblInfo.Text = "启用路由IP模式，不启用关机";
            }
            else
            {
                lblInfo.Text = "是否启用代理服务器INI参数配置有错误";
            }
            
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show(this, "最小化到托盘点“是” ,直接退出点“否” ", "退出程序", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                e.Cancel = true;
                this.WindowState = FormWindowState.Minimized;
                this.ShowInTaskbar = false; //不显示在系统任务栏 
                notifyIcon1.Visible = true; //托盘图标可见 
            }
            if (dr == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
            if (dr == DialogResult.No)
            {
                this.Close();
                // Application.Exit();
            }
        }

       

        #region  DataGridView事件处理部分
        private void dgvShow_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (this.dgvShow.Columns[e.ColumnIndex].Name == "shopIndex")
                {
                    string url = dgvShow.Rows[e.RowIndex].Cells["shopIndex"].Value.ToString();
                    Process.Start("iexplore.exe", url);
                }
            }
        }
        private void dgvShow_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int selectSum=0;
                bool x=dgvShow.Rows[e.RowIndex].Selected;
                foreach (DataGridViewRow dgvr in dgvShow.Rows)
                {
                    if (dgvr.Selected)
                        selectSum++;
                }
                MessageBox.Show("您选中了："+selectSum.ToString()+"--行");
            }
        }
        private void dgvShow_Sorted(object sender, EventArgs e)
        {
            int index = 1;
            foreach (DataGridViewRow dgvr in dgvShow.Rows)
            {
                dgvShow.Rows[index - 1].Cells[0].Value = index;
                index++;
            }
        }
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }
        #endregion DataGridView事件处理部分结束
        private void ksDp_URLShift(string url)
        {
            this.wb_Keysearch.Url = new Uri(url);
        }
        static double count = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            count++;
            double time = count / 60;
            lblUseTime.Text = time.ToString();
        }
        //private void wb_Keysearch_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        //{
        //    string x = e.Url.ToString();
        //    logger.LogToFile("url:"+x);
        //    using (StreamReader sr = new StreamReader(this.wb_Keysearch.DocumentStream, System.Text.Encoding.Default))
        //    {
        //        string htmlStr = sr.ReadToEnd();
        //        //ksDp.GetDianpu(htmlStr);
        //    }
        //}
        #endregion Form事件处理部分   结束
    }
}
